package com.yyh.fanshe;

import java.lang.reflect.Field;

public class Demo6 {
    public static void main(String[] args) throws Exception {
        //1.获取User.java文件
        Class clas1 = Class.forName("com.yyh.fanshe.User");
        //2.获取age属性对象
        Field ageFiled = clas1.getDeclaredField("age");
        //3.获取该属性对象的权限修饰符
        int modifiers = ageFiled.getModifiers();
        System.out.println("modifiers = " + modifiers);
        System.out.println("------------------------------------------------");
        //4.获取该属性对象的类型
        Class type = ageFiled.getType();
        System.out.println("type = " + type);
        System.out.println("------------------------------------------------");
        //5.获取该属性对象的名称
        String name = ageFiled.getName();
        System.out.println("name = " + name);
        System.out.println("------------------------------------------------");
        //6.获取该属性对象的属性值
        ageFiled.setAccessible(true);
        int o = (int) ageFiled.get(new User("张三", 18, "男", "北京"));
        System.out.println("o = " + o);
        System.out.println("------------------------------------------------");
        //7.修改当前对象的，这个age属性值为30
        User user = new User("张三", 18, "男", "北京");
        ageFiled.set(user, 30);
        int o1 = (int) ageFiled.get(user);
        System.out.println("o1 = " + o1);
        System.out.println("------------------------------------------------");
        //8.获取address属性对象
        Field addressField = clas1.getDeclaredField("address");
        //打开暴力访问开关
        addressField.setAccessible(true);
        //获取用户对象的私有属性address的值
        String address = (String) addressField.get(new User("张三", 18, "男", "北京"));
        System.out.println("address = " + address);
    }
}
